package bplus

import (
	"go.uber.org/atomic"
	"os"
)

/*
	BPlus Tree
*/
type Config struct {
	degree          int
	order           int
	pageSize        int
	available       int
	indexDataOffset int
	idxType         IndexType
	fullRewrite     bool
}

//type IndexType uint8
type OperateType int
type Operate[T uint64 | string] struct {
	node      *Node[T]
	idx       int
	operate   OperateType
	isNewNode bool
}

type Operates[T uint64 | string] map[uint64]*Operate[T]

type PageBuffer struct {
	buf       []byte
	pgid      uint64
	isNewPage bool
}
type MetaInfo struct {
	page       *Page
	pageNum    atomic.Uint64
	overflow   []uint64
	rewriteIdx int
	dirty      bool
}

type BPlusTree[T uint64 | string] struct {
	typeHold T
	fd       *os.File
	root     *Node[T]
	meta     *MetaInfo
	cache    map[uint64]*Node[T]
	operate  Operates[T]
	conf     *Config
}

/*
	Node
*/
type Item[T uint64 | string] struct {
	key   T
	value []byte
}

type Node[T uint64 | string] struct {
	pgid           uint64
	overflowPageId []uint64
	isLeaf         bool
	parent         *Node[T]
	key            []T
	item           []*Item[T]
	children       []*Node[T]
	next           *Node[T]
}

/*
	Page
*/
type IndexType uint8
type PageType uint8
type Page struct {
	Id       uint64
	pgType   PageType
	overflow uint64 // 数据溢出页
	next     uint64 // 下一页
	meta     *MetaPage
	index    *IndexPage
	leaf     *LeafPage
	Checksum uint32
}
type MetaPage struct {
	Degree   int
	Root     uint64
	MaxPage  uint64
	idxType  IndexType
	FreePage []uint64
}
type IndexPage struct {
	// Node *Node
}
type LeafPage struct {
	// Node *Node
}